package club.wx3609.images.controller;

import club.wx3609.images.pool.UserRegisterPool;
import club.wx3609.images.Utils.IsNullUtils;
import club.wx3609.images.entity.User;
import club.wx3609.images.interfaces.Constant;
import club.wx3609.images.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.*;

@Controller
public class RegisterController {

    @Resource
    private UserService userService;

    @RequestMapping("/register")
    public String registerPage(){
        return "Register";
    }

    @Transactional
    @ResponseBody
    @PostMapping("/registerUser")
    public String registerUser(String userName, String userPassword, HttpServletRequest request){
        Map<String,String> map=new HashMap<String,String>();
        String requestIP=request.getRemoteAddr();
        if((UserRegisterPool.map.get(requestIP))==null) {
            if (IsNullUtils.character(userName) && IsNullUtils.character(userPassword)) {
                boolean register = false;
                StringBuffer stringBuffer = new StringBuffer();
                do {
                    Random random = new Random();
                    int length = random.nextInt(4);
                    for (int i = 0; i < (length + 9); i++) {
                        stringBuffer.append(random.nextInt(10));
                    }
                    User user = userService.registerVerification(stringBuffer.toString());
                    if (user == null) {
                        register = true;
                    }
                } while (!register);
                User user0 = new User();
                user0.setUserName(userName);
                user0.setUserNumber(stringBuffer.toString());
                user0.setUserPassword(userPassword);
                user0.setRegisterState(Constant.registerState0);
                User user1 = userService.registerUser(user0);
                map.put("registerMessage", "您注册的账号为:" + user1.getUserNumber() + ";请等待管理员同意！");
                map.put("registerState", Constant.registerState0);
                UserRegisterPool.map.put(requestIP,String.valueOf((new Date().getTime())+5*60*1000));
                request.getSession().setAttribute("registerUserNumber",user1.getUserNumber());
                return JSON.toJSONString(map);
            }
        }else{
            map.put("registerMessage", Constant.registerTips0);
            map.put("registerState", Constant.registerState2);
            return JSON.toJSONString(map);
        }
        map.put("registerMessage",Constant.inputJudge0);
        map.put("registerState",Constant.registerState2);
        return JSON.toJSONString(map);
    }
}
